<template>
  <div class="stat-card" :class="{ 'stat-card--clickable': clickable }" @click="handleClick">
    <div class="stat-card__header">
      <div class="stat-card__icon" :style="{ backgroundColor: iconBg }">
        <el-icon :size="24" :color="iconColor">
          <component :is="icon" />
        </el-icon>
      </div>
      <div class="stat-card__info">
        <div class="stat-card__title">{{ title }}</div>
        <div class="stat-card__value">{{ formatValue(value) }}</div>
      </div>
    </div>
    <div v-if="subtitle" class="stat-card__subtitle">
      {{ subtitle }}
    </div>
    <div v-if="trend !== undefined" class="stat-card__trend">
      <el-icon :color="trendColor" :size="14">
        <ArrowUp v-if="trend > 0" />
        <ArrowDown v-if="trend < 0" />
        <Minus v-if="trend === 0" />
      </el-icon>
      <span :style="{ color: trendColor }">{{ Math.abs(trend) }}%</span>
      <span class="stat-card__trend-text">{{ trendText }}</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { ArrowUp, ArrowDown, Minus } from '@element-plus/icons-vue'

interface Props {
  title: string
  value: number | string
  subtitle?: string
  icon: string
  iconColor?: string
  iconBg?: string
  trend?: number
  trendText?: string
  clickable?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  iconColor: '#ffffff',
  iconBg: '#409eff',
  trendText: '较上周',
  clickable: false
})

const emit = defineEmits<{
  click: []
}>()

const trendColor = computed(() => {
  if (props.trend === undefined) return ''
  if (props.trend > 0) return '#67c23a'
  if (props.trend < 0) return '#f56c6c'
  return '#909399'
})

const formatValue = (value: number | string): string => {
  if (typeof value === 'string') return value
  if (value >= 10000) {
    return (value / 10000).toFixed(1) + 'w'
  }
  if (value >= 1000) {
    return (value / 1000).toFixed(1) + 'k'
  }
  return value.toString()
}

const handleClick = () => {
  if (props.clickable) {
    emit('click')
  }
}
</script>

<style scoped lang="scss">
.stat-card {
  background: #ffffff;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  border: 1px solid transparent;
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
    opacity: 0;
    transition: opacity 0.3s ease;
  }

  &--clickable {
    cursor: pointer;
    
    &:hover {
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
      transform: translateY(-4px);
      border-color: #f0f0f0;
      
      &::before {
        opacity: 1;
      }
      
      .stat-card__icon {
        transform: scale(1.1) rotate(5deg);
      }
      
      .stat-card__value {
        color: #667eea;
      }
    }
  }

  &__header {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
  }

  &__icon {
    width: 56px;
    height: 56px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 16px;
    transition: all 0.3s ease;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  &__info {
    flex: 1;
  }

  &__title {
    font-size: 14px;
    color: #666666;
    margin-bottom: 6px;
    font-weight: 500;
    letter-spacing: 0.3px;
  }

  &__value {
    font-size: 32px;
    font-weight: 700;
    color: #2c3e50;
    line-height: 1;
    transition: color 0.3s ease;
    background: linear-gradient(135deg, #2c3e50 0%, #667eea 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
  }

  &__subtitle {
    font-size: 13px;
    color: #999999;
    margin-bottom: 12px;
    padding: 8px 12px;
    background: #f8f9fa;
    border-radius: 6px;
    display: inline-block;
  }

  &__trend {
    display: flex;
    align-items: center;
    font-size: 13px;
    font-weight: 500;
    padding: 6px 12px;
    background: #f8f9fa;
    border-radius: 6px;
    display: inline-flex;
    
    .el-icon {
      margin-right: 4px;
    }

    &-text {
      margin-left: 6px;
      color: #999999;
      font-weight: 400;
    }
  }
}
</style>